package cn.saltybe.analysis.crawler.wbtc.wholeRentingHouse;

import cn.saltybe.analysis.crawler.InitGecco;
import com.geccocrawler.gecco.annotation.PipelineName;
import com.geccocrawler.gecco.pipeline.Pipeline;
import com.geccocrawler.gecco.request.HttpRequest;
import com.geccocrawler.gecco.scheduler.DeriveSchedulerContext;
import com.geccocrawler.gecco.scheduler.SchedulerContext;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.yaml.snakeyaml.util.UriEncoder;

@PipelineName("wbtcWholeRentingHouseListPipeline")
@Component
public class WbtcWholeRentingHouseListPipeline implements Pipeline<WbtcWholeRentingHouseList> {
    private static String cookie;

    @Value("${crawler.wbtc.cookie}")
    public void setCookie(String cookie) {
        this.cookie = cookie;
    }

    @Override
    public void process(WbtcWholeRentingHouseList wbtcWholeRentingHouseList) {
        HttpRequest currentRequest = wbtcWholeRentingHouseList.getRequest();
        String province = currentRequest.getParameter("province");
        String city = currentRequest.getParameter("city");
        System.out.println("正进行 " + province + " " + city + " 的整租房列表扫描，当前第 " + wbtcWholeRentingHouseList.getCurrentPage() + " 页");
        // 在这里进行抓取到信息的逻辑处理
        int subRequestCount = 0;
        for(String h: wbtcWholeRentingHouseList.getHouseList()) {
            //if (subRequestCount >= 1) break;
            //System.out.println("获取到整租房链接：" + h;
            HttpRequest subRequest = currentRequest.subRequest(h);
            subRequest.addParameter("province", currentRequest.getParameter("province"));
            subRequest.addParameter("city", currentRequest.getParameter("city"));
            subRequest.addHeader("cookie", cookie);
            // DeriveSchedulerContext.into(subRequest);
            SchedulerContext.into(subRequest);
            subRequestCount++;
        }
        System.out.println("已将 " + subRequestCount + " 个请求加入队列。");
        // 爬取下一页
        if (wbtcWholeRentingHouseList.getNextPageUrl() != null) {
            HttpRequest subRequest = currentRequest.subRequest(wbtcWholeRentingHouseList.getNextPageUrl());
            subRequest.addParameter("province", currentRequest.getParameter("province"));
            subRequest.addParameter("city", currentRequest.getParameter("city"));
            SchedulerContext.into(subRequest);
            System.out.println("已将下一页请求加入队列。");
        }
    }
}
